/**
 * Parse the parameters looking for a specific keyed array
 * In AWS requests, an array :
 *   Masters: ["Value1", "Value2"]
 *
 * Will be serialized into (note drop of 's' to Master):
 * params = {
 *  Master.1: "Value1"
 *  Master.2: "Value2"
 * }
 *
 * @param params serialized array of params
 * @param master key (should be given without the 's')
 * @param keys array of key names in each object
 */
var deserializeArray = function(params, name) {
    if (typeof params !== 'object' || typeof name !== 'string') {
        throw "IllegalArgumentException";
    }

    var paramArray = [];

    // Format for param map key is "<Master>.<index>"
    var regex = new RegExp("^" + name + "\.([\\d]+)$")
    for (var p in params) {
        var match = regex.exec(p);
        if (match) {
            var idx = (match[1] - 1);
            paramArray[idx] = params[p];
        }
    }

    return paramArray;
}

/**
 * Parse the parameters looking for a specific keyed array of objects
 * In AWS requests, an array of objects like:
 *   Masters: [{name:"Name1", value:"Value1"},{name:"Name2", value:"Value2}]
 *
 * Will be serialized into (note drop of 's' to Master):
 * params = {
 *  Master.1.name: "Name1"
 *  Master.1.value: "Value1"
 *  Master.2.name: "Name2"
 *  Master.2.value: "Value2"
 * }
 *
 * @param params serialized array of params
 * @param master key (should be given without the 's')
 * @param keys array of key names in each object
 */
var deserializeArrayMap = function(params, master, keys) {
    if (typeof params !== 'object' || typeof master !== 'string' || !Array.isArray(keys)) {
        throw "IllegalArgumentException";
    }
    var paramArray = [];
    // Format for param map key is "<master>.<index>.<key>"
    var regex = new RegExp("^" + master + "\.([\\d]+)\.([\\w]+)$")
    for (var p in params) {
        var match = regex.exec(p);
        if (match) {
            var idx = (match[1] - 1);
            var key = match[2];

            //We can't guarantee which order the keys are found, so create our object if needed
            paramArray[idx] = (paramArray[idx] || {});
            paramArray[idx][key] = params[p];
        }
    }

    return paramArray;
}

var deserializeMap = function(params) {
    if (typeof params !== 'object') {
        throw "IllegalArgumentException";
    }
    var paramRet = {};

    // Format for param map key is "<master>.<indexKey>.<indexKey>.<indexKey>..."
    var regex = new RegExp("^((?:[\\d\\w]+\\.?)+)$")
    for (var p in params) {
        var match = regex.exec(p);
        if (!match) {
            continue;
        }
        var keys = match[1].split('.');
        var carryForward = paramRet;

        var key = keys[0];
        for (var k = 0; k < keys.length - 1; k++) {
            if (isNaN(keys[k + 1])) {
                carryForward = (carryForward[key] = carryForward[key] || {});
                key = keys[k + 1];
            } else {
                carryForward = (carryForward[key] = carryForward[key] || []);
                key = keys[k + 1] - 1;
            }
        }
        carryForward[key] = params[p];
    }

    return paramRet;
}

exports.deserializeMap = deserializeMap;
